
package com.kugou.game.sdk.download;

import com.kugou.download.DefaultOperator;
import com.kugou.download.DownloadFile;
import com.kugou.download.IFileColumns;

import android.content.ContentValues;
import android.content.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 下载信息保存类
 * 
 * @author chenys
 */
public class DownloadInfoSaver extends DefaultOperator {

    private Context mContext;

    private DownloadOperator mDownloadOperator;

    public DownloadInfoSaver(Context context) {
        mContext = context;
        mDownloadOperator = DownloadOperator.getInstance();
    }

    /**
     * 新增文件信息
     * 
     * @param file 文件
     * @return 返回新增文件ID
     */
    public long insertFile(DownloadFile file) {
        return mDownloadOperator.insert(file);
    }

    /**
     * 更新文件信息
     * 
     * @param file 文件
     * @return 是否更新成功
     */
    public boolean updateFile(DownloadFile file) {
        return mDownloadOperator.update(file, file.getKey()) > 0;
    }

    @Override
    public boolean updateFile(HashMap<String, Object> updateFileds, String selection,
            String[] selectionArgs) {
        if (updateFileds != null && updateFileds.size() > 0) {
            ContentValues cv = new ContentValues();
            Set<String> keys = updateFileds.keySet();
            for (String key : keys) {
                if (IFileColumns.KEY.equals(key)) {
                    cv.put(IFileColumns.KEY, (String) updateFileds.get(key));
                } else if (IFileColumns.CLASSID.equals(key)) {
                    cv.put(IFileColumns.CLASSID, (Integer) updateFileds.get(key));
                } else if (IFileColumns.FILE_NAME.equals(key)) {
                    cv.put(IFileColumns.FILE_NAME, (String) updateFileds.get(key));
                } else if (IFileColumns.FILE_PATH.equals(key)) {
                    cv.put(IFileColumns.FILE_PATH, (String) updateFileds.get(key));
                } else if (IFileColumns.FILE_SIZE.equals(key)) {
                    cv.put(IFileColumns.FILE_SIZE, (Long) updateFileds.get(key));
                } else if (IFileColumns.RES_URL.equals(key)) {
                    cv.put(IFileColumns.RES_URL, (String) updateFileds.get(key));
                } else if (IFileColumns.HAVE_READ.equals(key)) {
                    cv.put(IFileColumns.HAVE_READ, (Long) updateFileds.get(key));
                } else if (IFileColumns.MIME_TYPE.equals(key)) {
                    cv.put(IFileColumns.MIME_TYPE, (String) updateFileds.get(key));
                } else if (IFileColumns.STATE.equals(key)) {
                    cv.put(IFileColumns.STATE, (Integer) updateFileds.get(key));
                } else if (IFileColumns.IS_DELETE.equals(key)) {
                    cv.put(IFileColumns.IS_DELETE, (Integer) updateFileds.get(key));
                } else if (IFileColumns.EXT1.equals(key)) {
                    cv.put(IFileColumns.EXT1, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT2.equals(key)) {
                    cv.put(IFileColumns.EXT2, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT3.equals(key)) {
                    cv.put(IFileColumns.EXT3, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT4.equals(key)) {
                    cv.put(IFileColumns.EXT4, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT5.equals(key)) {
                    cv.put(IFileColumns.EXT5, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT6.equals(key)) {
                    cv.put(IFileColumns.EXT6, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT7.equals(key)) {
                    cv.put(IFileColumns.EXT7, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT8.equals(key)) {
                    cv.put(IFileColumns.EXT8, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT9.equals(key)) {
                    cv.put(IFileColumns.EXT9, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT10.equals(key)) {
                    cv.put(IFileColumns.EXT10, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT11.equals(key)) {
                    cv.put(IFileColumns.EXT11, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT12.equals(key)) {
                    cv.put(IFileColumns.EXT12, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT13.equals(key)) {
                    cv.put(IFileColumns.EXT13, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT14.equals(key)) {
                    cv.put(IFileColumns.EXT14, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT15.equals(key)) {
                    cv.put(IFileColumns.EXT15, (String) updateFileds.get(key));
                } else if (IFileColumns.EXT16.equals(key)) {
                    cv.put(IFileColumns.EXT16, (String) updateFileds.get(key));
                }
            }
            return mDownloadOperator.update(cv, selection, selectionArgs) > 0;
        } else {
            return false;
        }
    }

    /**
     * 删除文件信息
     * 
     * @param id 文件ID
     * @return 是否删除成功
     */
    public boolean deleteFile(long id) {
        return mDownloadOperator.delete(id) > 0;
    }

    /**
     * 删除文件信息
     */
    public boolean deleteFile(String key) {
        return mDownloadOperator.delete(key) > 0;
    }

    /**
     * 查询文件信息
     * 
     * @param id 文件ID
     * @return 返回文件信息
     */
    public DownloadFile queryFile(long id) {
        return mDownloadOperator.query(id);
    }

    /**
     * 查询文件信息
     * 
     * @param selection 查询条件
     * @param selectionArgs 查询条件赋值
     * @return 返回文件信息集合
     */
    public List<DownloadFile> queryFile(String selection, String[] selectionArgs) {
        return mDownloadOperator.query(selection, selectionArgs, null);
    }

    /**
     * 查询文件信息
     * 
     * @param selection 查询条件
     * @param selectionArgs 查询条件赋值
     * @param orderby 排序条件
     * @return 返回文件信息集合
     */
    public List<DownloadFile> queryFile(String selection, String[] selectionArgs, String orderby) {
        return mDownloadOperator.query(selection, selectionArgs, orderby);
    }

    /**
     * 查询数量
     * 
     * @param selection 查询条件
     * @param selectionArgs 查询条件赋值
     * @return 返回数量
     */
    public int getCount(String selection, String[] selectionArgs) {
        return mDownloadOperator.getCount(selection, selectionArgs);
    }

}
